<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从文件指针中读入一行并解析 CSV 字段</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.fgetc.html">« fgetc</a></li>
      <li style="float: right;"><a href="function.fgets.html">fgets »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.filesystem.html">文件系统函数</a></li>
    <li>从文件指针中读入一行并解析 CSV 字段</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.fgetcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fgetcsv</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">fgetcsv</span> &mdash; <span class="dc-title">从文件指针中读入一行并解析 CSV 字段</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.fgetcsv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fgetcsv</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code>null</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$separator</code><span class="initializer"> = &quot;,&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &quot;\&quot;&quot;</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$escape</code><span class="initializer"> = &quot;\\&quot;</span></span><br>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   和 <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span> 类似，只除了
   <span class="function"><strong>fgetcsv()</strong></span> 解析读入的行并找出 <abbr title="Comma Separated Value">CSV</abbr>
   格式的字段然后返回一个包含这些字段的数组。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    使用此函数需要考虑区域设置。例如，如果 <code class="literal">LC_CTYPE</code> 是
    <code class="literal">en_US.UTF-8</code>，通过该函数可能会导致对文件中的单字节编码读取错误。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fgetcsv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">
       一个由 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>、<span class="function"><a href="function.popen.html" class="function">popen()</a></span> 或
       <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> 产生的有效文件指针。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       必须大于在 CVS 文件（允许尾随行尾字符）中找到的最长行（以字符为单位）。否则，该行将拆分为
       <code class="parameter">length</code> 字符的块，除非拆分发生在环绕字符内。
      </p>
      <p class="para">
       忽略此参数（或者设为 0 或者在 PHP 8.0.0 及以后的版本中设为 <strong><code>null</code></strong>）行的最大长度将不受限制，速度稍慢。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">separator</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">separator</code> 参数，设置字段分隔符（仅一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enclosure</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">enclosure</code> 参数，设置字段环绕符（仅一个单字节字符）。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">escape</code></dt>

     <dd>

      <p class="para">
       可选的 <code class="parameter">escape</code>
       参数，设置转义字符（最多一个单字节字符）。空字符串（<code class="literal">&quot;&quot;</code>）禁用所有的转义机制。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        Usually an <code class="parameter">enclosure</code> character is escaped inside
        a field by doubling it; however, the <code class="parameter">escape</code>
        character can be used as an alternative. So for the default parameter
        values <code class="literal">&quot;&quot;</code> and <code class="literal">\&quot;</code> have the same
        meaning. Other than allowing to escape the
        <code class="parameter">enclosure</code> character the
        <code class="parameter">escape</code> character has no special meaning; it isn&#039;t
        even meant to escape itself.
       </span>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fgetcsv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回包含读取字段的索引数组， 或者在失败时返回 <strong><code>false</code></strong>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">    
    CSV 文件中的空行将被返回为一个包含有单个 <span class="type">null</span> 字段的数组，不会被当成错误。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">在读取在 Macintosh 电脑中或由其创建的文件时， 如果 PHP
不能正确的识别行结束符，启用运行时配置可选项 <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
也许可以解决此问题。</span></p></blockquote>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fgetcsv-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        现在 <code class="parameter">length</code> 允许为 null.
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        <code class="parameter">escape</code> 参数也接受空字符串来禁用所有的转义机制。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fgetcsv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2152">
    <p><strong>示例 #1 读取并显示 CSV 文件的整个内容</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />if&nbsp;((</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"test.csv"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgetcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">","</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #DD0000">&nbsp;fields&nbsp;in&nbsp;line&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #DD0000">:&nbsp;&lt;br&nbsp;/&gt;&lt;/p&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$c</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #0000BB">$c</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fgetcsv-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.str-getcsv.html" class="function" rel="rdfs-seeAlso">str_getcsv()</a> - 解析 CSV 字符串为一个数组</span></li>
    <li class="member"><span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
    <li class="member"><span class="function"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
    <li class="member"><span class="function"><a href="function.pack.html" class="function" rel="rdfs-seeAlso">pack()</a> - 将数据打包成二进制字符串</span></li>
    <li class="member"><span class="function"><a href="function.fputcsv.html" class="function" rel="rdfs-seeAlso">fputcsv()</a> - 将行格式化为 CSV 并写入文件指针</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>